import { Request, Response, NextFunction } from "express";
import { connection } from "@/services/database";
import { checkIsStudentBelongUser, checkIsAdmin } from "@/utils/sql";

interface Params {
	user_id: number;
}
export default async (request: Request, response: Response, next: NextFunction) => {
	try {
		const params = request.query as unknown as Params;
		const callerId = response.locals.user.id;
		if (callerId != params.user_id && !(await checkIsStudentBelongUser(callerId, params.user_id)) && !(await checkIsAdmin(callerId))) throw new Error("无权操作");
		const [results] = (await connection.execute("SELECT user_id, contact_phone_number, school_code, real_name, gender, birth_date, identification_number FROM student WHERE user_id = :user_id", params)) as any;
		if (!results[0]) response.json({ data: null, message: "未找到该学生" });
		else
			response.json({
				data: {
					user_id: results[0].user_id,
					contact_phone_number: results[0].contact_phone_number,
					school_code: results[0].school_code,
					real_name: results[0].real_name,
					gender: results[0].gender,
					birth_date: results[0].birth_date,
					identification_number: results[0].identification_number,
				},
				message: "成功",
			});
	} catch (error) {
		response.statusCode = 400;
		next(error);
	}
};
